import 'dart:collection';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meePet/app/Constants.dart';
import 'package:meePet/manager/RouteManager.dart';
import 'package:meePet/net/RequestManager.dart';
import 'package:meePet/platform/PlatformPlugin.dart';
import 'package:meePet/utils/LogUtils.dart';
import 'package:meePet/utils/ext/SizeExt.dart';
import 'package:meePet/utils/ext/StringExt.dart';

import '../../app/LocalColor.dart';
import '../../app/RouteConstants.dart';
import '../../base/BaseActivity.dart';
import '../../bean/GeekRegisterData.dart';
import '../../generated/l10n.dart';
import '../../platform/PluginCallBack.dart';

class LoginPage extends BaseActivity {
  @override
  State<StatefulWidget> createState() {
    return LoginState();
  }
}

class LoginState extends BaseActivityState {
  @override
  Widget initView(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: const SystemUiOverlayStyle(
        /*statusBarColor: Colors.transparent,//状态栏颜色
          statusBarIconBrightness: Brightness.dark, //状态栏图标颜色
          statusBarBrightness: Brightness.dark,  //状态栏亮度
          systemStatusBarContrastEnforced: true, //系统状态栏对比度强制*/

        systemNavigationBarColor: LocalColor.colorTheme, //导航栏颜色
        systemNavigationBarIconBrightness: Brightness.dark, //导航栏图标颜色
        systemNavigationBarDividerColor: LocalColor.colorTheme, //系统导航栏分隔线颜色
        systemNavigationBarContrastEnforced: false, //系统导航栏对比度强制
      ),
      child: getScaffold(context),
    );
  }

  Widget getScaffold(BuildContext context) {
    return Scaffold(
      appBar: null,
      body: _contentView(),
    );
  }

  Widget _contentView() {
    return Stack(
      children: [
        Container(
          width: 375.sw(),
          height: double.infinity,
          child:
              "common_page_bg".webp().loadImageRes(375.sw(), double.infinity),
        ),
        GestureDetector(
          onTap: _clickBack,
          child: Container(
            margin: EdgeInsets.only(left: 12.sw(), top: 44.sw()),
            child: "common_back_black".png().loadImageRes(30.sw(), 30.sw()),
          ),
        ),
        _getCenterPanel(),
      ],
    );
  }

  Widget _getCenterPanel() {
    return Column(
      children: [
        Container(
          alignment: Alignment.topCenter,
          margin: EdgeInsets.only(top: 199.sw()),
          child: "bg_login_title".png().loadImageRes(223.sw(), 27.sw()),
        ),
        _inputPhoneView(),
        Container(
          alignment: Alignment.topCenter,
          margin: EdgeInsets.only(top: 25.sw()),
          child: MaterialButton(
            onPressed: () {
              _doForValidPhone();
            },
            color: LocalColor.color_ff947bff,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(22.sw())),
            height: 44.sw(),
            minWidth: 325.sw(),
            textColor: LocalColor.colorWhite,
            child: Text(
              S.current.getSMCode,
              style: TextStyle(color: LocalColor.colorWhite, fontSize: 16.sp()),
            ),
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: 30.sw()),
          child: _policeAndServiceBar(),
        ),
      ],
    );
  }

  Widget _inputPhoneView() {
    return Container(
      height: 50.sw(),
      width: 305.sw(),
      decoration: BoxDecoration(
          color: Colors.white,
          shape: BoxShape.rectangle,
          border: Border.all(color: LocalColor.colorWhite20, width: 1.sw()),
          borderRadius: BorderRadius.all(Radius.circular(25.sw()))),
      margin: EdgeInsets.only(top: 45.sw(), left: 35.sw(), right: 35.sw()),
      child: Row(
        children: [
          Container(
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(left: 10.sw()),
            child: "icon_phone".png().imageRes(w: 22.sw(), h: 22.sw()),
          ),
          Expanded(
              child: Container(
            width: 100.sw(),
            height: 44.sw(),
            margin: EdgeInsets.only(left: 10.sw(), right: 10.sw()),
            child: TextField(
              textAlign: TextAlign.start,
              decoration: InputDecoration(
                  border: InputBorder.none, hintText: S.current.inputPhone),
              keyboardType: TextInputType.phone,
              onChanged: (value) {},
              onEditingComplete: () {},
            ),
          )),
        ],
      ),
    );
  }

  Widget _policeAndServiceBar() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text("我已阅读并同意"),
        GestureDetector(
          onTap: () {
            _goWebView(Constants.H5_USER_PROTOCOL);
          },
          child: Text(
            "《用户服务协议》",
            style: TextStyle(color: LocalColor.color_ff947bff),
          ),
        ),
        Text("和"),
        GestureDetector(
          onTap: () {
            _goWebView(Constants.H5_USER_PRIVATE);
          },
          child: Text("《隐私政策》",
              style: TextStyle(color: LocalColor.color_ff947bff)),
        ),
      ],
    );
  }

  void _clickBack() {
    LogUtils.i("点击了返回");
    //RouteManager.back();
    RouteManager.off(RouteConstants.mainPage);
  }

  void _goWebView(String url) {
    Map<String, String> params = Map();
    params["url"] = url;
    RouteManager.to(RouteConstants.webPage, mParams: params);
  }

  @override
  void onCreate() {
    //_doForValidPhone()

    super.onCreate();
  }

  void _doForValidPhone() {
    LogUtils.i("初始化极验");
    RequestManager.getUserClient()
        .geekRegister()
        .then((value) => {_dealWithInitGeeTestData(value)});
  }

  void _dealWithInitGeeTestData(GeekRegisterData value) {
    LogUtils.i("初始化极验获取的结果:${value.toJson()}");
    if (value.data != null) {
      PlatformPlugin.getSMS("15625059241", dataToJson(value.data!),
          PluginCallBack((Object o) {
        LogUtils.i("返回的结果是啥：" + o.toString());
      }));
    }
  }
}
